package com.moon.game.util;

import java.util.Random;

public class IDGenerator {
	private static Random random = new Random();

	/**
	 * 主键ID生成 时间+随机（1000~9999）
	 */
	public static long generateLongId() {
		int ext = random.nextInt(9000) + 1000;
		String id = System.currentTimeMillis() + String.valueOf(ext);
		return Long.parseLong(id);
	}
	
	/**
	 * 主键ID生成 时间(9位)+key(指定)+随机(4位1000~9999)
	 */
	public static long generateId(int key) {
		int ext = random.nextInt(9000) + 1000;
		String currentTime = String.valueOf(System.currentTimeMillis());
		String tempId = currentTime.substring(6, currentTime.length());
		if (tempId.startsWith("0")) {
			tempId = tempId.replaceFirst("0", String.valueOf(random.nextInt(9) + 1));
		}
		String id = tempId + key + ext;
		return Long.parseLong(id);
	}
	
	
	/**
	 * 生成int型随机数
	 * 
	 * @return
	 */
	public static int generateIntId() {
		String currentTime = String.valueOf(System.currentTimeMillis());
		String tempId = currentTime.substring(5, currentTime.length());
		if (tempId.startsWith("0")) {
			tempId = tempId.replaceFirst("0", String.valueOf(random.nextInt(9) + 1));
		}
		Integer id = Integer.parseInt(tempId);
		return id;
	}
	
}
